<template>
  <div class="app-container">
    <el-row :gutter="24">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="账号名称" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入账号名称"
              clearable
              size="small"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="商品名称" prop="status">
            <el-select
              v-model="queryParams.type"
              placeholder="商品名称"
              clearable
              size="small"
              style="width: 200px"
            >
              <el-option v-for="(item,index) in goodsList" :key="item.id" :label="item.goodsName" :value="item.id"/>

            </el-select>
          </el-form-item>
          <el-form-item label="领取时间起" prop="status">
            <el-col :span="11">
              <el-date-picker type="date"  value-format="yyyy-MM-dd"  placeholder="选择日期" v-model="queryParams.startTime" style="width: 200px;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="领取时间止" prop="status">
            <el-col :span="11">
              <el-date-picker type="date"  value-format="yyyy-MM-dd"  placeholder="选择日期" v-model="queryParams.endTime" style="width: 200px;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>

          <!-- <right-toolbar
            :show-search.sync="showSearch"
            :columns="columns"
            @queryTable="getList"
          /> -->
        </el-row>

        <el-table :data="exchangeList" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column prop="userName" label="账号名称" align="center"/>
          <el-table-column  v-if="false" prop="id" label="id" align="center" hidden/>
          <el-table-column prop="goodsName" label="商品名称" align="center"/>
          <el-table-column prop="goodsPoint" label="消耗积分" align="center"/>
          <el-table-column prop="receiveFlag" label="是否领取" align="center">
            <template slot-scope="scope">
              <el-switch
                :value="scope.row.receiveFlag"
                active-value="1"
                inactive-value="0"
                @change="handleStatusChange(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column prop="exchangeTime" label="领取时间" align="center">
            <template slot-scope="scope">
              {{ scope.row.receiveFlag === '0' ? '' : scope.row.exchangeTime }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="兑换时间" align="center"/>

        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="账号名称" prop="userName">
              <el-input
                v-model="form.userName"
                placeholder="请输入用户账号"
                maxlength="30"
              />
            </el-form-item>

            <el-form-item label="积分值" prop="score">
              <el-input
                v-model="form.score"
                placeholder="请输入积分值"
                type="number"
                maxlength="30"
              />
            </el-form-item>

            <el-form-item label="积分来源" prop="scoreDetail">
              <el-input
                v-model="form.scoreDetail"
                placeholder="请输入积分来源"
                maxlength="30"
              />
            </el-form-item>


          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getToken } from '@/utils/auth'
import {
  allExchangeList,
  changeExchangeStatus,
  delExchange
}
  from '@/api/point/exchange'
import {allGoodsList} from "@/api/point/goods";

export default {
  name: 'Exchange',
  data() {
    let self = this;
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        type: undefined,
        startTime:undefined,
        endTime:undefined
      },
      queryGoods:{
        pageNum: 1,
        pageSize: 999
      },
      title: '',
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      exchangeList: null,
      goodsList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {}
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 用户状态修改
    handleStatusChange(row) {
      const text = row.receiveFlag === '0' ? '领取' : '修改';
      this.$modal
        .confirm('确认将该条记录改为'+text+'状态')
        .then(function() {
          return changeExchangeStatus(row.id, row.receiveFlag === '0'?'1':'0').then((response) => {
            row.receiveFlag = row.receiveFlag === '0'?'1':'0'
          })
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功');
          this.getList();
        })
        .catch(function() {
          row.receiveFlag = row.receiveFlag === '0' ? '1' : '0'
        })
    },
    getList() {
      this.loading = true
      allExchangeList(this.queryParams).then((response) => {
        this.exchangeList = response.list
        this.total = response.total
        this.loading = false
      });
      allGoodsList(this.queryGoods).then((response) => {
        this.goodsList = response.list
      });
    },

    // 表单重置
    reset() {
      this.form = {
        userName: undefined,
        score: undefined,
        scoreDetail: undefined,
        type: undefined,
        userId: undefined,
        id:undefined
      }
      this.resetForm('form')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否删除当前选中数据项？')
        .then(function () {
          return delExchange(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if(this.form.score < 0 ){
            this.form.type = "1"
          }else{
            this.form.type = "0"
          }
          addPoint(this.form).then((response) => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
